﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace 图书管理系统
{
    class BorrowDao
    {
        public int input_Borrowrecord(String ReaderID,String BookNum,String Identy)
        {
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            SqlConnection conn = new SqlConnection();        
            string sql1 = "insert into BorrowinfoTable(BookNum,ReaderID,Borrow_time,Return_time,BorrowState) values(@BookNum,@ReaderID,@Borrow_time,@Return_time,@BorrowState)";
            SqlCommand cmd4 = Dao.SqlConnect(sql1, conn);
           
            
            cmd4.Parameters.Add("@BookNum", SqlDbType.Char, 20).Value = BookNum;
            cmd4.Parameters.Add("@ReaderID", SqlDbType.Char,15).Value = ReaderID;
            cmd4.Parameters.Add("@Borrow_time", SqlDbType.DateTime).Value = currentTime;
            cmd4.Parameters.Add("@Return_time", SqlDbType.DateTime).Value = rule(Identy, currentTime);
            cmd4.Parameters.Add("@BorrowState", SqlDbType.Char, 10).Value = "借阅中";
    
            try
            {
                conn.Open();
                if(cmd4.ExecuteNonQuery()>0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return 0;
        }
        public String count()
        {
            SqlConnection conn = new SqlConnection();
            String sql = "select count(*) from BorrowinfoTable";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            String num=null ;
            
            while(reader.Read())
            {
                num =reader[0].ToString()+1;
            }
            conn.Close();
            return num;
        }
        public DateTime rule(String Identy,DateTime currentTime)
        {
            int BorrowTerm=0;
            SqlConnection conn = new SqlConnection();
            String sql = "select BorrowTerm  from Borrowingrules_Table where Identy='" + Identy+"'";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
            {
                BorrowTerm = (int)reader[0];

            }
            conn.Close();
            return currentTime = currentTime.AddDays(BorrowTerm);
        }
        public int juage(String ReaderID,String BookNum)
        {
            SqlConnection conn = new SqlConnection();
            String sql = "select ReaderID  from Appointmentrecord_Table where BookNum='"+BookNum+"'and OrderState='预约中' order by Order_time asc";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
            {
                if(reader[0].ToString().Trim()==ReaderID.Trim())
                {
                    conn.Close();
                    return 1;
                }
                else
                {
                    conn.Close();
                    return 0;
                }
            }
            return 0;
        }
    }
}
